<template>
<!-- 滚动事件只能给滚动盒子加 -->
  <div class="page" @scroll="scroll">
    <header v-show="showHeader">
      <img @click="gotop" src="../../assets/img/jingtian.jpeg" alt="">
    </header>
    <div class="banner">
      <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
        <van-swipe-item v-for="(item, index) in bannerList" :key="index">
          <img :src="item.picUrl" alt="" />
        </van-swipe-item>
      </van-swipe>
      <ul>
        <li>
          <img src="../../assets/img/love.png" alt="" />
          <p>签到</p>
        </li>
        <li>
          <img src="../../assets/img/lv.png" alt="" />
          <p>礼券</p>
        </li>
        <li>
          <img src="../../assets/img/wg.png" alt="" />
          <p>砍价</p>
          <!-- 行内写的px不会被转换成rem -->
        </li>
        <li>
          <img src="../../assets/img/dp.png" alt="" />
          <p>专栏</p>
        </li>
      </ul>
    </div>
    <my-title>全民砍价</my-title>

    <my-title>精选专题</my-title>
    <div class="news">
      <ul>
        <li v-for="(item, index) in newsList" :key="index">
          <img :src="item.pic" alt="" />
          <h6>{{ item.title }}</h6>
          <p>{{ item.descript }}</p>
        </li>
      </ul>
    </div>
    <my-title @click="jump">人气推荐</my-title>
    <div class="people">
       <my-cart v-for="(item,index) in goodsList" :key="index" :itemObj="item"></my-cart>
    </div>
  </div>
</template>

<script>
import { getBanner, getNews,getGoods } from "@/api/home/index";
import { ref, reactive, toRefs } from "vue";
import { useRouter } from 'vue-router';
import MyTitle from "../../components/myTitle.vue";
import MyCart from '../../components/myCart.vue';
export default {
  name: "Home",
  components: { MyTitle, MyCart },
  setup() {
    const bannerList = ref([]); // 轮播图的数据
    getBanner().then((res) => {
      bannerList.value = res.data;
    });

    const data = reactive({
      newsList: [], // 新闻列表
      goodsList: [], // 商品列表
      showHeader: false, // 是否显示头部
    });

    getNews().then((res) => {
      // 新闻的请求
      data.newsList = res.data;
    });
  
  // page 要显示的页码  pageSize 每页显示条数
   getGoods({page:3,pageSize:4}).then(res => { // 获取商品的数据的第三页
      data.goodsList = res.data
      // console.log(data.goodsList);
   })
   

   let dom = null;
  //  const scollDom = ref(null);
   let top = 0;
   const scroll = (e) => { // e 事件对象
     // scrollTop 滚动条距离顶部的高度
    //  console.log(e.target.scrollTop);
    dom = e.target;
    top = e.target.scrollTop; // 记录滚动高度
    if(e.target.scrollTop > 400){
       data.showHeader = true
    } else {
      data.showHeader = false
    }
   }

   const gotop = () => { // 返回顶部的函数
    //  dom.scrollTop = 0;
    // scollDom.value.scrollTop = 0;
    // console.log(scollDom.value);
    let time = setInterval(()=>{
       if (top>0){
         top = top - 10;
         dom.scrollTop = top
       } else{
         clearInterval(time)
       }
    },1)
   }
   const router = useRouter(); // 获取 router 对象
  //  console.log(router);
   const jump = () => { // 人气列表跳转
     router.push('/people');
   }
    return {
      bannerList,
      ...toRefs(data),
      scroll,
      gotop,
      jump
    };
  },
};
</script>
<style lang="scss" scoped>
.my-swipe .van-swipe-item {
  img {
    width: 100%;
  }
}
header{
  height: 50px;
  position: fixed;
  top:0;
  left: 0;
  background: #fff;
  width: 100%;
  z-index: 99;
  display: flex;
  justify-content: center;
  align-items: center;
  img{
    height: 80%;
    border-radius: 5px;
  }
}
.banner {
  position: relative;
  ul {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    background: #fff;
    padding: 10px 0;
    border-radius: 20% 20% 0 0;
    li {
      width: 25%;
      text-align: center;
      font-size: 16px;
      display: inline-flex;
      height: 80px;
      flex-wrap: wrap;
      img {
        width: 50%;
        display: block;
        margin: 0 auto;
      }
      p {
        width: 100%;
        align-self: flex-end;
      }
    }
  }
}
.news {
  width: 100%;
  overflow-x: auto;
  ul {
    display: flex;
    width: calc(5 * 329px);
    background: #fff;
    padding-bottom: 10px;
    li {
      width: 319px;
      margin-left: 10px;
      font-size: 14px;
      img {
        width: 100%;
        border-radius: 5px;
        height: 213px;
      }
      p,
      h6 {
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      h6{
        margin-top: 10px;
      }
      p{
        color: #A8A8A8;
        font-size: 12px;
        margin-top: 10px;
      }
    }
  }
}
.people{
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  background: #fff;
  flex-wrap: wrap;
}
</style>